﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;

namespace Comm
{
    public static class IOHelp
    {
        static System.IO.Ports.SerialPort Port = new System.IO.Ports.SerialPort();

        public static string wechatId = "";

        public static int counts = 0;
        public static int AlertTime = 1000;
        public static bool IsAlert = false;


        /// <summary>
        /// 串口
        /// </summary>
        /// <param name="com"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static string Open(string com, string baudRate)
        {


            try
            {
                if (!Port.IsOpen)
                {

                    Port.BaudRate = int.Parse(baudRate);
                    Port.PortName = com;
                    Port.Open();
                    IsAlert = false;
                    return "ok";
                }
                else
                {
                    return "ok";
                }
            }
            catch (Exception e)
            {
                string exce = e.Message;
                return exce;

            }
        }


        /// <summary>
        /// 发送端口位开
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static bool sendOpen(string mes, int washType)
        {

                //发送数据       
                if (!Port.IsOpen)
                {
                    Port.PortName = mes;
                    Port.BaudRate = 9600;
                    Port.Open();
                    // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
                }

                byte[] strbyte1 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x01, 0x00, 0x00, 0x00, 0xB2 };
                byte[] strbyte2 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x02, 0x00, 0x00, 0x00, 0xB3 };
                byte[] strbyte3 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x03, 0x00, 0x00, 0x00, 0xB4 };
                byte[] strbyte4 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x04, 0x00, 0x00, 0x00, 0xB5 };
                byte[] strbyte5 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x05, 0x00, 0x00, 0x00, 0xB6 };
                byte[] strbyte6 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x06, 0x00, 0x00, 0x00, 0xB7 };
                byte[] strbyte7 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x07, 0x00, 0x00, 0x00, 0xB8 };
                byte[] strbyte8 = new byte[] { 0x00, 0x5A, 0x56, 0x01, 0x08, 0x00, 0x00, 0x00, 0xB9 };
                byte[] strbyte9 = new byte[] { 0x00, 0x5A, 0x56, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB4 };
                byte[] strbyte = null;
            switch (washType)
            {
                case 0:
                    strbyte = strbyte1;
                    break;
                case 1:
                    strbyte = strbyte2;
                    break;
                case 2:
                    strbyte = strbyte3;
                    break;
                case 3:
                    strbyte = strbyte4;
                    break;
                case 4:
                    strbyte = strbyte5;
                    break;
                case 5:
                    strbyte = strbyte6;
                    break;
                case 6:
                    strbyte = strbyte7;
                    break;
                case 7:
                    strbyte = strbyte8;
                    break;
                case 8:
                    strbyte = strbyte9;
                    break;
            }
            lock (ojb)
            {
                Port.Write(strbyte, 0, strbyte.Length);
                Thread.Sleep(30);
            }
               
            
            return true;
        }
        private static object ojb = new object();
        /// <summary>
        /// 发送端口位关
        /// </summary>
        /// <param name="mes"></param>
        /// <returns></returns>
        public static bool sendClose(string mes, int washType)
        {

            //发送数据       
            if (!Port.IsOpen)
            {
                Port.PortName = mes;
                Port.BaudRate = 9600;
                Port.Open();
                // Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Port_DataReceived);
            }

            byte[] strbyte1 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x01, 0x00, 0x00, 0x00, 0xB3 };
            byte[] strbyte2 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x02, 0x00, 0x00, 0x00, 0xB4 };
            byte[] strbyte3 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x03, 0x00, 0x00, 0x00, 0xB5 };
            byte[] strbyte4 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x04, 0x00, 0x00, 0x00, 0xB6 };
            byte[] strbyte5 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x05, 0x00, 0x00, 0x00, 0xB7 };
            byte[] strbyte6 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x06, 0x00, 0x00, 0x00, 0xB8 };
            byte[] strbyte7 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x07, 0x00, 0x00, 0x00, 0xB9 };
            byte[] strbyte8 = new byte[] { 0x00, 0x5A, 0x56, 0x02, 0x08, 0x00, 0x00, 0x00, 0xBA };
            byte[] strbyte9 = new byte[] { 0x00, 0x5A, 0x56, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB4 };
            byte[] strbyte = null;
            switch (washType)
            {
                case 0:
                    strbyte = strbyte1;
                    break;
                case 1:
                    strbyte = strbyte2;
                    break;
                case 2:
                    strbyte = strbyte3;
                    break;
                case 3:
                    strbyte = strbyte4;
                    break;
                case 4:
                    strbyte = strbyte5;
                    break;
                case 5:
                    strbyte = strbyte6;
                    break;
                case 6:
                    strbyte = strbyte7;
                    break;
                case 7:
                    strbyte = strbyte8;
                    break;
                case 8:
                    strbyte = strbyte9;
                    break;
            }
            lock (ojb)
            {
                Port.Write(strbyte, 0, strbyte.Length);
                Thread.Sleep(30);
            }
            return true;
        }



        public static bool Close()
        {
            if(Port.IsOpen)
            Port.Close();

            return true;
        }
    }
}
